package com.yisu.auction.ewf.common.utils;

import java.math.BigDecimal;

/**
 * Created by nichole on 16/8/27.
 */
public class MapUtil {

    public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param longitude1 第一点经度
     * @param latitude1  第一点纬度
     * @param longitude2 第二点经度
     * @param latitude2  第二点纬度
     * @return 返回距离 单位：KM
     */
    public static double distance(double longitude1, double latitude1, double longitude2,
                                  double latitude2) {
        double a, b, R;
        R = 6378137; // 地球半径
        latitude1 = latitude1 * Math.PI / 180.0;
        latitude2 = latitude2 * Math.PI / 180.0;
        a = latitude1 - latitude2;
        b = (longitude1 - longitude2) * Math.PI / 180.0;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        double d = (2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(latitude1)
                * Math.cos(latitude2) * sb2 * sb2))) / 1000;

        BigDecimal decimal=   new   BigDecimal(d);
        return  decimal.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 根据经纬度计算两点之间的方向, 两点不能为同一个点
     *
     * @param startLongitude  起点经度
     * @param startLatitude   起点纬度
     * @param endLongitude    终点经度
     * @param endLatitude     终点纬度
     */
    public static MapDirection direction(double startLongitude, double startLatitude, double endLongitude,
                                 double endLatitude) {
        MapDirection direction;
        double lgtDiff = endLongitude - startLongitude;
        double latDiff = endLatitude - startLatitude;

        if (lgtDiff == 0) {
            if(latDiff >= 0) {
                direction = MapDirection.NORTH;
            } else {
                direction = MapDirection.SOUTH;
            }
        } else if(lgtDiff > 0) {
            if(latDiff == 0) {
                direction = MapDirection.EAST;
            } else if(latDiff > 0) {
                direction = MapDirection.NORTHEAST;
            } else {
                direction = MapDirection.SOUTHEAST;
            }
        } else {
            if(latDiff == 0) {
                direction = MapDirection.WEST;
            } else if(latDiff > 0) {
                direction = MapDirection.NORTHWEST;
            } else {
                direction = MapDirection.SOUTHWEST;
            }
        }

        return direction;
    }

    /**
     * 将火星坐标系(GCJ-02)坐标转换成百度坐标系(BD-09)坐标
     *
     * @param lon 经度
     * @param lat 伟度
     * @return
     */
    public static double[] gcj02_To_Bd09(double lon, double lat) {
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta) + 0.0065;
        double tempLat = z * Math.sin(theta) + 0.006;
        double[] gps = {tempLon, tempLat};
        return gps;
    }


    /**
     * 将百度坐标系(BD-09)坐标转换成火星坐标系(GCJ-02)坐标 * * @param
     *
     * @param lon 经度
     * @param lat 伟度
     * @return
     */
    public static double[] bd09_To_Gcj02(double lon, double lat) {
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta);
        double tempLat = z * Math.sin(theta);
        double[] gps = {tempLon, tempLat};
        return gps;
    }


    public static void main(String[] args) {
        double[] a = bd09_To_Gcj02(120.42266849339074, 28.98596871729976);
        System.out.println(a[0] + "," + a[1]);
        System.out.println(distance(a[0], a[1],120.41505400000, 28.98540800000));
    }


    public enum MapDirection {
        EAST, SOUTH, WEST, NORTH, NORTHEAST, SOUTHEAST, NORTHWEST, SOUTHWEST
    }

}


